package cn.ruanyun.backInterface.modules.business.orderAfterSale.controller;

import cn.ruanyun.backInterface.common.enums.OrderStatusEnum;
import cn.ruanyun.backInterface.common.exception.RuanyunException;
import cn.ruanyun.backInterface.common.utils.ResultUtil;
import cn.ruanyun.backInterface.common.utils.SecurityUtil;
import cn.ruanyun.backInterface.common.utils.ToolUtil;
import cn.ruanyun.backInterface.common.vo.PageVo;
import cn.ruanyun.backInterface.common.vo.Result;
import cn.ruanyun.backInterface.modules.base.pojo.User;
import cn.ruanyun.backInterface.modules.base.service.mybatis.IUserService;
import cn.ruanyun.backInterface.modules.business.order.pojo.Order;
import cn.ruanyun.backInterface.modules.business.order.service.IOrderService;
import cn.ruanyun.backInterface.modules.business.orderAfterSale.dto.OrderAfterCommitDto;
import cn.ruanyun.backInterface.modules.business.orderAfterSale.dto.OrderAfterSaleDto;
import cn.ruanyun.backInterface.modules.business.orderAfterSale.pojo.OrderAfterSale;
import cn.ruanyun.backInterface.modules.business.orderAfterSale.service.IOrderAfterSaleService;
import cn.ruanyun.backInterface.modules.business.storeIncome.service.IStoreIncomeService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Optional;

/**
 * @author wj
 * 售后申请管理接口
 */
@Slf4j
@RestController
@RequestMapping("/ruanyun/orderAfterSale")
@Transactional
@Api(tags = "售后管理接口")
public class OrderAfterSaleController {


    @Autowired
    private IOrderAfterSaleService iOrderAfterSaleService;

    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private IStoreIncomeService storeIncomeService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IUserService userService;



    @PostMapping("/commitOrderAfterSale")
    @ApiOperation("提交售后")
    public Result<Object> commitOrderAfterSale(OrderAfterCommitDto orderAfterCommitDto) {

       return iOrderAfterSaleService.commitOrderAfterSale(orderAfterCommitDto);
    }


    @PostMapping("/resolveOrderAfterSale")
    @ApiOperation("商家处理售后订单(&&管理员强制退款")
    public Result<Object> resolveOrderAfterSale(OrderAfterSaleDto orderAfterSaleDto) {


        //店铺收入
        BigDecimal storeIncome = storeIncomeService.getStoreIncomeByOrderId(Optional.ofNullable(orderService.getById(orderAfterSaleDto.getOrderId()))
                .map(Order::getUserId).orElse(null), orderAfterSaleDto.getOrderId());

        return Optional.ofNullable(iOrderAfterSaleService.getByOrderId(orderAfterSaleDto.getOrderId()))
               .map(orderAfterSale -> Optional.ofNullable(orderService.getById(orderAfterSale.getOrderId())).map(order -> {

                   if (!OrderStatusEnum.SALE_AFTER.equals(order.getOrderStatus())) {

                       return new ResultUtil<>().setErrorMsg(209, "该订单已经处理过！");
                   }

                    //1. 获取当前商家的保证金
                    BigDecimal depositMoney = Optional.ofNullable(userService.getById(order.getUserId())).map(User::getDepositMoney)
                            .orElse(new BigDecimal(0));

                    if (ToolUtil.isNotEmpty(orderAfterSaleDto.getActualRefundMoney())) {

                        if (orderAfterSaleDto.getActualRefundMoney().compareTo(storeIncome) > 0) {

                            return new ResultUtil<>().setErrorMsg(205, "实际退余额不能大于收入余额！");

                        }

                        if (depositMoney.compareTo(orderAfterSaleDto.getActualRefundMoney()) < 0) {

                            return new ResultUtil<>().setErrorMsg(206, "保证金不足，请前往app充值保证金");
                        }

                    }else {

                        if (depositMoney.compareTo(storeIncome) < 0) {

                            return new ResultUtil<>().setErrorMsg(206, "保证金不足，请前往app充值保证金");
                        }

                    }

                   iOrderAfterSaleService.resolveOrderAfterSale(orderAfterSaleDto);
                   return new ResultUtil<>().setSuccessMsg("处理退款成功！");
                }).orElse(new ResultUtil<>().setErrorMsg(203, "当前订单不存在！"))).orElse(new ResultUtil<>().setErrorMsg(202, "当前退款订单不存在！"));
    }


    @PostMapping("/revocationAfterOrder")
    @ApiOperation("撤销售后")
    public Result<Object> revocationAfterOrder(String orderId) {

        return iOrderAfterSaleService.revocationAfterOrder(orderId);
    }

    @PostMapping("/appOrderAfterSaleDetail")
    @ApiOperation("app通过订单id获取售后信息")
    public Result<Object> appOrderAfterSaleDetail(String orderId) {
        return Optional.ofNullable(iOrderAfterSaleService.getByOrderId(orderId))
                .map(orderAfterSaleVo -> new ResultUtil<>().setData(orderAfterSaleVo,"操作成功！"))
                .orElse(new ResultUtil<>().setErrorMsg(201,"暂无数据！"));
    }

    @PostMapping("/resolveReceiveReturnMoney")
    @ApiOperation("确认收到退款")
    public Result<Object> resolveReceiveReturnMoney(String orderAfterSaleId) {

       iOrderAfterSaleService.resolveReceiveReturnMoney(orderAfterSaleId);

       return new ResultUtil<>().setSuccessMsg("确认收到退款成功！");
    }

    @PostMapping("offlineReturnMoney")
    @ApiOperation("处理线下退款")
    public Result<Object> offlineReturnMoney(String orderId) {

        return iOrderAfterSaleService.offlineReturnMoney(orderId);

    }

}
